package com.mail.server.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mail.dubbo.api.PmsProductApi;
import com.mail.dubbo.api.SmsFlashPromotionProductRelationApi;
import com.mail.model.db.PmsProduct;
import com.mail.model.db.SmsFlashPromotionProductRelation;
import com.mail.model.vo.PageResult;
import com.mail.model.vo.PmsProVo;
import com.mail.model.vo.PmsProductVo;
import com.mail.server.common.R;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author yan
 * @date 2022/06/21 10:14
 **/
@Service
public class SmsFlashPromotionProductRelationService {
    @DubboReference
    private SmsFlashPromotionProductRelationApi smsFlashPromotionProductRelationApi;

    public R<String> create(List<SmsFlashPromotionProductRelation> smsFlashPromotionProductRelation) {
        smsFlashPromotionProductRelationApi.create(smsFlashPromotionProductRelation);
        return R.success("操作成功");
    }

    public R delete(Long id) {

        smsFlashPromotionProductRelationApi.delete(id);
        return R.success("删除成功");
    }

    @DubboReference
    private PmsProductApi pmsProductApi;


    /**根据id更新
     * @param id
     * @param relation
     * @return
     */
    public R update(Long id, SmsFlashPromotionProductRelation relation) {
        smsFlashPromotionProductRelationApi.update(id,relation);
        return R.success("");
    }

    public R list(Long flashPromotionId, Long flashPromotionSessionId, Long pageNum, Long pageSize) {
        //从关系表查询出ids和sort
        SmsFlashPromotionProductRelation smsFlashPromotionProductRelation = new SmsFlashPromotionProductRelation();
        smsFlashPromotionProductRelation.setFlashPromotionId(flashPromotionId.longValue());
        smsFlashPromotionProductRelation.setFlashPromotionSessionId(flashPromotionSessionId.longValue());
        Page list = smsFlashPromotionProductRelationApi.list(smsFlashPromotionProductRelation, pageNum, pageSize);
        List<SmsFlashPromotionProductRelation> records = list.getRecords();
        List<Long> productIds = CollUtil.getFieldValues(records, "productId", Long.class);
        List<PmsProVo> pmsProductVos = new ArrayList<>();
        //根据ids获得所有的产品
        Map<Long, PmsProduct> productMap = pmsProductApi.getProductByIds(productIds);
        for (SmsFlashPromotionProductRelation record : records) {
            PmsProduct pmsProduct = productMap.get(record.getProductId());
            if (pmsProduct != null) {
                PmsProVo pmsProductVo = new PmsProVo();
                pmsProductVo.setProduct(pmsProduct);
                BeanUtils.copyProperties(record,pmsProductVo);
                pmsProductVos.add(pmsProductVo);
            }
        }
        PageResult pageResult = new PageResult(pageNum, pageSize, list.getTotal(), pmsProductVos);
        return R.success(pageResult);
    }

    public R getProductionMessage(Long id) {
        SmsFlashPromotionProductRelation relation=smsFlashPromotionProductRelationApi.getProductionMessage(id);
        return R.success(relation);
    }
}
